package com.t235.gc.auth.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserDetailsImpl implements UserDetailsService {
    @Resource
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //我这里弄一个简单的权限集合，真实的开发中是要查询数据库的
        List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("admin,p1");

        return new MyUserDetails(user.getUsername(), user.getPassword(), auth);
    }
}
